package com.scuola.scuoladocente;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MenuActivity extends Activity {
	
	private ListView lv;
	private JSONArray parsedList = null;
	private JSONParser parser = new JSONParser();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        
        TextView tv = (TextView)findViewById(R.id.tv_intestazione);
        TextView tv_date = (TextView)findViewById(R.id.tv_intestazione_data);
        lv = (ListView)findViewById(R.id.lv_classi);
        ArrayList<String> lvItems = new ArrayList<String>();
        
        try {
        	
	        AsyncTask<Void, Void, String> getProfDataResponse = new getProfData().execute();
			parsedList = (JSONArray) parser.parse(getProfDataResponse.get());
			JSONObject obj = (JSONObject) parser.parse(parsedList.get(0).toString());
						
			tv.setText("Docente: "+obj.get("nome")+" "+obj.get("cognome"));			
			tv_date.setText("Data: "+DateFormat.getDateTimeInstance().format(new Date()));
			
			AsyncTask<Void, Void, String> getClassByProfIdResponse = new getClassByProfId().execute();
			parsedList = (JSONArray) parser.parse(getClassByProfIdResponse.get());
			
			for(int i = 0; i < parsedList.size(); i++ ){
				obj = (JSONObject) parser.parse(parsedList.get(i).toString());
				lvItems.add(obj.get("classe").toString());
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvItems);
			lv.setAdapter(adapter);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					JSONObject selectedObj = (JSONObject) parser.parse(parsedList.get(arg2).toString());
	            	Intent SelectionActivity = new Intent(getApplicationContext(), SelectionActivity.class);
	            	SelectionActivity.putExtra("idclasse", selectedObj.get("idclasse").toString());
	            	SelectionActivity.putExtra("nomeClasse",selectedObj.get("classe").toString());
	            	SelectionActivity.putExtra("idutente", getIntent().getStringExtra("id_utente"));
	            	startActivityForResult(SelectionActivity, 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
    }
    
    private class getProfData extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("id", getIntent().getStringExtra("id_utente"));
				String strResponse = operations.wsCall("getDataById", hm).toString();
				return strResponse;
				
			} catch (Exception e) {
				System.err.println("getProfData errore (Exception)");
				e.printStackTrace();
				this.doInBackground(arg0);
				return null;
			}
		}
    }
    
    private class getClassByProfId extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("id", getIntent().getStringExtra("id_utente"));
				String strResponse = operations.wsCall("getClassByProfId", hm).toString();
				return strResponse;
				
			} catch (Exception e) {
				System.err.println("getClassByProfId errore (Exception)");
				e.printStackTrace();
				this.doInBackground(arg0);
				return null;
			}
		}
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Exit:
            	System.exit(0);
            	break;
        }
		return true;
    }
    
}
